﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.Revit.Electric.Common
{
	public class ViewUtils
	{
		public static bool IsViewType(View view, CmdViewType viewType)
		{
			if (view == null)
			{
				return false;
			}
			ViewType viewType2 = view.ViewType;
			switch ((int)viewType2)
			{
			case 1:
				return (viewType & CmdViewType.FloorPlan) > CmdViewType.Other;
			case 2:
				return (viewType & CmdViewType.CeilingPlan) > CmdViewType.Other;
			case 3:
				return (viewType & CmdViewType.Elevation) > CmdViewType.Other;
			case 4:
				return (viewType & CmdViewType.ThreeD) > CmdViewType.Other;
			case 5:
				return (viewType & CmdViewType.Schedule) > CmdViewType.Other;
			case 6:
				return (viewType & CmdViewType.DrawingSheet) > CmdViewType.Other;
			case 7:
			case 8:
			case 9:
				break;
			case 10:
				return (viewType & CmdViewType.DraftingView) > CmdViewType.Other;
			case 11:
				return (viewType & CmdViewType.Legend) > CmdViewType.Other;
			default:
                switch ((int)viewType2)
				{
				case 115:
					return (viewType & CmdViewType.EngineeringPlan) > CmdViewType.Other;
				case 116:
					return (viewType & CmdViewType.AreaPlan) > CmdViewType.Other;
				case 117:
					return (viewType & CmdViewType.Section) > CmdViewType.Other;
				}
				break;
			}
			return false;
		}

		public static List<ElementId> GetViewsOpened(UIDocument uiDoc, CmdViewType viewType)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (UIView uiview in uiDoc.GetOpenUIViews())
			{
				if (ViewUtils.IsViewType(uiDoc.Document.GetElement(uiview.ViewId) as View, viewType))
				{
					list.Add(uiview.ViewId);
				}
			}
			return list;
		}

		public static List<ElementId> GetViews(Document doc, CmdViewType viewType)
		{
			List<ElementId> list = new List<ElementId>();
			FilteredElementIterator elementIterator = new FilteredElementCollector(doc).OfClass(typeof(View)).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				View view = element as View;
				if (view != null && !view.IsTemplate && ViewUtils.IsViewType(view, viewType))
				{
					list.Add(view.Id);
				}
			}
			return list;
		}

		public static bool ElementIsShowInView(Document doc, ElementId entId, BuiltInCategory category, ElementId viewId)
		{
			if (doc == null || null == entId || null == viewId)
			{
				return false;
			}
			Element element = doc.GetElement(entId);
			return element != null && new FilteredElementCollector(doc, viewId).OfCategory(category).OfClass(element.GetType()).ToElementIds().Contains(entId);
		}

		public static bool ElementIsShowInView(Document doc, ElementId entId, ElementId viewId)
		{
			if (doc == null || null == entId || null == viewId)
			{
				return false;
			}
			Element element = doc.GetElement(entId);
			if (element == null)
			{
				return false;
			}
            BuiltInCategory integerValue = (BuiltInCategory)element.Category.Id.IntegerValue;
			return new FilteredElementCollector(doc, viewId).OfCategory(integerValue).OfClass(element.GetType()).ToElementIds().Contains(entId);
		}
	}
}
